package com.wecon.dianping.service.impl;

import com.wecon.dianping.common.BusinessException;
import com.wecon.dianping.common.EmBusinessError;
import com.wecon.dianping.dal.ShopMapper;
import com.wecon.dianping.model.Category;
import com.wecon.dianping.model.Seller;
import com.wecon.dianping.model.Shop;
import com.wecon.dianping.model.ShopExample;
import com.wecon.dianping.service.CategoryService;
import com.wecon.dianping.service.SellerService;
import com.wecon.dianping.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author Weikang  Wang
 * @program: com.wecon.dianping.service.impl
 * @description:
 * @date 2020/3/18 10:07
 */
@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SellerService sellerService;

    /***
     * 创建门店
     * @param shop /
     * @return /
     */
    @Override
    @Transactional
    public Shop create(Shop shop) throws BusinessException {

        // 1.校验商家是否存在
        Seller seller = sellerService.get(shop.getSellerId());
        if (seller == null) {
            throw new BusinessException(EmBusinessError.PARMETER_VALIDATION_ERROR, "商家不存在");
        }
        // 商户是否被禁用
        if (seller.getDisabledFlag() == 1) {
            throw new BusinessException(EmBusinessError.PARMETER_VALIDATION_ERROR, "商户已禁用");
        }
        // 2.校验品类
        Category category = categoryService.getCategory(shop.getCategoryId());
        if (category == null) {
            throw new BusinessException(EmBusinessError.PARMETER_VALIDATION_ERROR, "品类不存在");
        }

        // 3.创建门店
        shopMapper.insertSelective(shop);

        return getShop(shop.getId());
    }

    /**
     * 根据id获取门店（包含商户信息和品类信息）
     * @param id id
     * @return /
     */
    @Override
    public Shop getShop(Integer id) {
        Shop shop = shopMapper.selectByPrimaryKey(id);
        if (shop == null) {
            return null;
        }

        shop.setSeller(sellerService.get(shop.getSellerId()));
        shop.setCategory(categoryService.getCategory(shop.getCategoryId()));

        return shop;
    }

    /**
     * 获取所有
     * @return /
     */
    @Override
    public List<Shop> selectAll() {
        List<Shop> shopList = shopMapper.selectAll();
        return getShopListByForEach(shopList);
    }

    /**
     * 获取门店数量
     * @return /
     */
    @Override
    public Integer countAllShop() {
        ShopExample example = new ShopExample();
        example.or().andIdGreaterThan(0);
        return shopMapper.countByExample(example);
    }

    /**
     * 推荐v1.0
     * @param longitude 经度
     * @param latitude 纬度
     * @return /
     */
    @Override
    public List<Shop> recommend(BigDecimal longitude, BigDecimal latitude) {
        List<Shop> shopList = shopMapper.recommend(longitude, latitude);
        return getShopListByForEach(shopList);
    }

    /**
     * 搜索v1.0
     * @param longitude /
     * @param latitude /
     * @param keyword 关键字
     * @return /
     */
    @Override
    public List<Shop> search(BigDecimal longitude, BigDecimal latitude, String keyword, Integer categoryId, Integer orderby, String tags) {
        List<Shop> shopList = shopMapper.search(longitude, latitude, keyword, categoryId, orderby, tags);
        return getShopListByForEach(shopList);
    }

    /**
     * 搜索标签
     * @param keyword /
     * @param categoryId /
     * @param tags /
     * @return /
     */
    @Override
    public List<Map<String, Object>> searchGroupByTags(String keyword, Integer categoryId, String tags) {
        return shopMapper.searchGroupByTags(keyword, categoryId, tags);
    }

    /**
     * 对shopList进行操作
     * @param shopList /
     * @return /
     */
    private List<Shop> getShopListByForEach(List<Shop> shopList) {
        shopList.forEach(shop -> {
            shop.setCategory(categoryService.getCategory(shop.getCategoryId()));
            shop.setSeller(sellerService.get(shop.getSellerId()));
        });

        return shopList;
    }

}
